﻿using System;
using System.Windows;
using System.Windows.Forms;
using System.Windows.Input;
using WControls = System.Windows.Controls;
using DataBaseLibrary.DBObjectsFB;
using KeyEventArgs = System.Windows.Input.KeyEventArgs;
using UserControl = System.Windows.Controls.UserControl;

namespace DocMreoLiteVersion.Controls
{

    /// <summary>
    /// Логика реализации транспортного компонента
    /// </summary>
    public partial class TransportControl : UserControl
    {

        public static readonly DependencyProperty HeaderWindowProperty =
            DependencyProperty.Register("HeaderWindow", typeof(String), typeof(TransportControl));

        public String HeaderWindow
        {
            set { DataContext = value; SetValue(HeaderWindowProperty, value); }
            get { return (String)GetValue(HeaderWindowProperty); }
        }


        private Vechicle vechicleObject;
        

        private enum INPUT_LANGUAGE
        {
            ENGLISH = 0,
            RUSSIAN = 1
        }

        public Vechicle VechicleObject
        {
            set
            {
                DataContext = value;
                vechicleObject = value;
            }
            get { return vechicleObject; }
        }


        public TransportControl()
        {
            InitializeComponent();
        }

        private void SetInputLanguage(INPUT_LANGUAGE inputLanguage)
        {
            if (inputLanguage == INPUT_LANGUAGE.RUSSIAN)
                InputLanguage.CurrentInputLanguage = InputLanguage.FromCulture(new System.Globalization.CultureInfo("ru-RU"));
            if (inputLanguage == INPUT_LANGUAGE.ENGLISH)
                InputLanguage.CurrentInputLanguage = InputLanguage.FromCulture(new System.Globalization.CultureInfo("en-US"));
        }

        private void ChangeLanguage_OnGotFocus(object sender, RoutedEventArgs e)
        {
            SetInputLanguage(INPUT_LANGUAGE.ENGLISH);
        }

        private void ChangeLanguage_OnLostFocus(object sender, RoutedEventArgs e)
        {
            SetInputLanguage(INPUT_LANGUAGE.RUSSIAN);
        }


        private void CopyVinToKuzov(string text)
        {
            vechicleObject.OTechnicalCharacteristics.NumberCarcass = text;
        }

        private void UIElement_OnKeyDown(object sender, KeyEventArgs e)
        {

            if (sender == VinTextBox && e.Key == Key.F5)
            {
                WControls.TextBox textBox = (WControls.TextBox)sender;
                if (textBox != null)
                    CopyVinToKuzov(textBox.Text);
            }

            if (sender == DopVinTextBox && e.Key == Key.F5)
            {
                WControls.TextBox textBox = (WControls.TextBox)sender;
                if (textBox != null)
                    CopyVinToKuzov(textBox.Text);
            }
            
            if (e.Key == Key.Enter)
            {
                const FocusNavigationDirection focusNav = FocusNavigationDirection.Next;
                var request = new TraversalRequest(focusNav);

                //ComboBox <Enter>
                if ((sender as WControls.ComboBox) != null && (sender as WControls.ComboBox).IsEditable)
                {
                    (sender as WControls.ComboBox).IsEditable = false;
                    (sender as UIElement).MoveFocus(request);
                    (sender as WControls.ComboBox).IsEditable = true;

                    e.Handled = true;
                    return;
                }

                if ((sender as UIElement) != null)
                    (sender as UIElement).MoveFocus(request);
                
                e.Handled = true;
            }
        }
    }
}
